package com.credithc.signature.manager.service;

import com.alibaba.fastjson.JSONObject;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.mapper.ChannelMapper;
import com.credithc.signature.manager.client.CompanyClient;
import com.credithc.signature.manager.init.CompanyInit;
import com.credithc.signature.manager.ro.CompanyRo;
import com.credithc.signature.manager.ro.ProductRo;
import com.credithc.signature.manager.utils.PinyinComparator;
import com.credithc.signature.manager.vo.CompanyProductVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zhq
 */
@Service
@Slf4j
public class CompanyService {

    @Autowired
    private CompanyInit companyInit;

    @Autowired
    private CompanyClient companyClient;



    public PageInfo<CompanyRo> getCompanyList() {

        log.info("开始请求saaas数据，时间{}",new Date());
        Message<List<CompanyRo>> cacheData = companyClient.getCacheData();
        log.info("结束请求saaas数据，时间{}",new Date());
        List<CompanyRo> companyVos = cacheData.getData();
        List<String> comNames = new ArrayList<>();
        Map<String,CompanyRo> comMap = new HashMap<>();
        for (CompanyRo cr:companyVos) {
            comNames.add(cr.getCompanyName());
            comMap.put(cr.getCompanyName(),cr);
        }
        System.out.println(comNames);
        Object[] comNames1 = comNames.toArray();
        Arrays.sort(comNames1, new PinyinComparator());
        System.out.println(JSONObject.toJSONString(comNames1));

        List<CompanyRo> companyRos = new ArrayList<>();
        for (int i = 0;i<comNames1.length ;i++) {
            companyRos.add( comMap.get(comNames1[i]));
        }

        PageInfo<CompanyRo> pageInfo = new PageInfo<>();
        pageInfo.setList(companyRos);
        return pageInfo;
    }

    @Autowired
    private ChannelMapper channelMapper;

    public PageInfo<ProductRo> getProductList(Integer companyName) {
        log.info("开始请求saaas数据，时间{}",new Date());
        Message<List<CompanyRo>> cacheData = companyClient.getCacheData();
        log.info("结束请求saaas数据，时间{}",new Date());
        List<CompanyRo> companyVos = cacheData.getData();
        List<ProductRo> productRos = new ArrayList<>();
        List<String> productNames = new ArrayList<>();
        Map<String,ProductRo> productRoMap = new HashMap<>();
        for (CompanyRo c:companyVos) {
            if (companyName == null || companyName.equals(c.getCompanyId())){
                List<ProductRo> products = c.getProducts();
                for (ProductRo p:products) {
                    productNames.add(p.getProductName());
                    productRoMap.put(p.getProductName(),p);
                }
            }
        }
        Object[] proNames = productNames.toArray();
        Arrays.sort(proNames, new PinyinComparator());
        for (int i = 0;i<proNames.length ;i++) {
            productRos.add( productRoMap.get(proNames[i]));
        }

        PageInfo<ProductRo> pageInfo = new PageInfo<>();
        pageInfo.setList(productRos);
        return pageInfo;
    }

    public PageInfo<ProductRo> getUserList() {
        List<ProductRo> companyVos = getProductRos();
        List<ProductRo> productVos = new ArrayList<>();
        Set<Long> set = new HashSet<>();
        for (ProductRo pv: companyVos) {
            if (set.add(pv.getUserId())){
                productVos.add(pv);
            }
        }
        PageInfo<ProductRo> pageInfo = new PageInfo<>();
        pageInfo.setList(productVos);
        return pageInfo;
    }

    private List<ProductRo> getProductRos() {
        log.info("开始请求saaas数据，时间{}",new Date());
        Message<List<CompanyRo>> cacheData = companyClient.getCacheData();
        log.info("结束请求saaas数据，时间{}",new Date());
        List<CompanyRo> companyVos = cacheData.getData();
        List<ProductRo> products = new ArrayList<>();
        for (CompanyRo cv:companyVos) {
            for (ProductRo pv:cv.getProducts()) {
                pv.setCompanyId(cv.getCompanyId());
                pv.setCompanyName(cv.getCompanyName());
                products.add(pv);
            }
        }
        return products;
    }


    public CompanyProductVo getProductMap(String code, Integer companyId, Integer userId){
        List<ProductRo> products =getProductRos();
        Map<String,ProductRo> proMap = new HashMap<>(16);
        List<String> channelIds = new ArrayList<>();
        for (ProductRo pr:products) {
            proMap.put(pr.getCode(),pr);
            if (StringUtils.isNotBlank(code) || companyId !=null || userId != null){
                Boolean b = true;
                if (StringUtils.isNotBlank(code) && !code.equals(pr.getCode())){
                    b= false;
                }
                if (companyId != null && !companyId.equals(pr.getCompanyId())){
                    b=false;
                }
                if (userId != null && !userId.equals(pr.getUserId())){
                    b =false;
                }
                if (b){
                    channelIds.add(pr.getCode());
                }
            }
        }
        return new CompanyProductVo(proMap,channelIds);
    }
}
